package com.bwf.lgqingdan.gui.activity;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.entity.ResponseHotKeywords;
import com.bwf.lgqingdan.gui.adapter.SearchHistoryAdapter;
import com.bwf.lgqingdan.gui.adapter.base.BaseRecycleViewAdapter;
import com.bwf.lgqingdan.mvp.presenter.SearchPresenter;
import com.bwf.lgqingdan.mvp.presenter.impl.SearchPresenterImpl;
import com.bwf.lgqingdan.mvp.view.SearchView;
import com.bwf.lgqingdan.utils.IntentUtils;
import com.bwf.lgqingdan.utils.KeyBoardUtils;
import com.lg.flowlayout.FlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/19.
 * 搜索界面
 */

public class SearchActivity extends ActivityBase implements SearchView, BaseRecycleViewAdapter.OnItemClickListener<String> {
    @BindView(R.id.textView_button_cancel)
    TextView textViewButtonCancel;
    @BindView(R.id.editText_search)
    EditText editTextSearch;
    @BindView(R.id.button_search)
    ImageButton buttonSearch;
    @BindView(R.id.root)
    LinearLayout root;
    @BindView(R.id.flowLayout_keyword)
    FlowLayout flowLayoutKeyword;
    @BindView(R.id.recyclerView_search_history)
    RecyclerView recyclerViewSearchHistory;
    @BindView(R.id.textView_clear_history)
    TextView textViewClearHistory;
    @BindView(R.id.layout_history)
    View layoutHistory;
    @BindView(R.id.img_search_delete)
    ImageView imgSearchDelete;
   private SearchPresenter presenter; //##################################################################

    private SearchHistoryAdapter searchHistoryAdapter;
    LinearLayoutManager linearLayoutManager;
    @Override
    public int getContentViewResID() {
        return R.layout.activity_search;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        root.setPadding(0, getStatusHeight(), 0, 0);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerViewSearchHistory.setLayoutManager(linearLayoutManager);

        editTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    doSearch(v.getText().toString());
                }
                return false;
            }
        });
        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                   hideDeleteImg();
                }else{
                   showDeleteImg();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        recyclerViewSearchHistory.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyBoardUtils.closeKeybord(editTextSearch,SearchActivity.this);
                return false;
            }
        });
    }

    /**
     * 执行搜索
     *
     * @param keyword
     */
    private void doSearch(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            IntentUtils.gotoSearchResultActivity(this,keyword);
            presenter.addKeyword(keyword);
        }
    }
    @Override
    protected void initDatas() {
        searchHistoryAdapter = new SearchHistoryAdapter(this);
        searchHistoryAdapter.setOnItemClickListener(this);
        recyclerViewSearchHistory.setAdapter(searchHistoryAdapter);

        presenter = new SearchPresenterImpl(this, this);
        presenter.loadHotKeywords();
        presenter.loadHistoryKeywords();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showLoadHotKeywordsFailed(String failedMsg) {

    }


    @Override
    public void clickSearch() {

    }

    @Override
    public void clickDeleteImg() {
        editTextSearch.getText().clear();
    }

    @Override
    public void clickCancle() {
        finish();
    }

    @Override
    public void showDeleteImg() {
        imgSearchDelete.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideDeleteImg() {
        imgSearchDelete.setVisibility(View.GONE);
    }

    @Override
    public void clickHotKeywordItem(ResponseHotKeywords.DataBean.KeywordsBean keywordBean) {
        doSearch(keywordBean.getName());
    }

    @Override
    public void clickHistoryTagItem(String keyword) {
        doSearch(keyword);
    }

    @Override
    public void showHotSearchKeywords(List<ResponseHotKeywords.DataBean.KeywordsBean> keywords) {
        for (int i = 0; i < keywords.size(); i++) {
            View tagView = getLayoutInflater().inflate(R.layout.list_item_tagview_item, flowLayoutKeyword, false);
            TagViewHolder holder = new TagViewHolder(tagView);
            holder.keywordBean = keywords.get(i);
            holder.tvTagItemContain.setText(keywords.get(i).getName());
            flowLayoutKeyword.addView(tagView);
        }
    }

    @Override
    public void showHistoryKeywords(List<String> historyKeywords) {
        searchHistoryAdapter.setDatas(historyKeywords);
        layoutHistory.setVisibility(View.VISIBLE);
    }

    @Override
    public void clickClearHistoryKeywords() {
        presenter.ClearHistoryKeywords();
        searchHistoryAdapter.clearDatas();
        showNoHistory();
    }

    @Override
    public void refreshHistoryKeywords(List<String> keywords) {
        searchHistoryAdapter.setDatas(keywords);
        layoutHistory.setVisibility(View.VISIBLE);
    }

    @Override
    public void showNoHistory() {
        layoutHistory.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.img_search_delete,R.id.textView_button_cancel, R.id.textView_clear_history})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_search_delete:
                clickDeleteImg();
                break;
            case R.id.textView_button_cancel:
                clickCancle();
                break;
            case R.id.textView_clear_history:
                clickClearHistoryKeywords();
                break;
        }
    }

    @Override
    public void onItemClick(int position, View view, String data) {
        clickHistoryTagItem(data);
    }

    class TagViewHolder implements View.OnClickListener {
        @BindView(R.id.tv_tag_item_contain)
        TextView tvTagItemContain;
        ResponseHotKeywords.DataBean.KeywordsBean keywordBean;

        TagViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickHotKeywordItem(keywordBean);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        editTextSearch.requestFocus();
        linearLayoutManager.scrollToPosition(0);
    }
}
